package cn.zhf.shop.service.impl;

import cn.zhf.shop.entity.ConsoleUser;
import cn.zhf.shop.entity.Message;
import cn.zhf.shop.mapper.ConsoleUserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import cn.zhf.shop.entity.AgentAudit;
import cn.zhf.shop.mapper.AgentAuditMapper;
import cn.zhf.shop.service.AgentAuditService;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

@Service
public class AgentAuditServiceImpl implements AgentAuditService {

    @Resource
    private AgentAuditMapper agentAuditMapper;
    @Resource
    private ConsoleUserMapper consoleUserMapper;


    @Override
    public Message deleteByPrimaryKey(Integer agentAuditId) {
        return agentAuditMapper.deleteByPrimaryKey(agentAuditId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(AgentAudit record) {
        return agentAuditMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(AgentAudit record) {
        return agentAuditMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer agentAuditId) {
        return Message.success(null).add(agentAuditMapper.selectByPrimaryKey(agentAuditId));
    }

    @Override
    public Message updateByPrimaryKeySelective(AgentAudit record) {
        return agentAuditMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(AgentAudit record) {
        return agentAuditMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectAll() {
        return Message.success(null).add(agentAuditMapper.selectALL());
    }

    @Override
    public Message selectALLNoZero() {
        return Message.success(null).add(agentAuditMapper.selectALLNoZero());
    }

    @Override
    public Message updateStatus(int agentAuditUserId, int agentAuditId, int Auditstatus,String rejectionReason) {
        AgentAudit agentAudit = agentAuditMapper.selectByPrimaryKey(agentAuditId);

        if (Auditstatus == 2) {
            if (agentAuditMapper.updateAgentAuditUserIdAndAuditPostStatusByAgentAuditId(agentAuditUserId, 2, agentAuditId) > 0) {
                if (agree(agentAudit)) {
                    return Message.success("操作成功！");
                }
            } else return Message.fail("更改审核状态失败");

        } else if (Auditstatus == 3) {
            return agentAuditMapper.updateAgentAuditUserIdAndAuditPostStatusAndRejectionReasonByAgentAuditId(agentAuditUserId, 3, rejectionReason,agentAuditId) > 0 ? Message.success(null) : Message.fail(null);
        }
        return Message.fail("审核状态输入错误！");

    }

    @Override
    public Message mohuSearch(String str) {
        return Message.success(null).add(agentAuditMapper.selectByRealname(str));
    }

    @Override
    public Message mohuSearchByPostUserId(String str,Integer postUserId) {
        return Message.success(null).add(agentAuditMapper.selectByPostUserIdAndRealnameMohu(postUserId,str));
    }

    @Override
    public Message selectByPostUserIdAndAuditPostStatus(Integer postUserId, Integer auditPostStatus) {
        return Message.success(null).add(agentAuditMapper.selectByPostUserIdAndAuditPostStatus(postUserId, auditPostStatus));
    }

    @Override
    public Message updateAuditPostStatusByAgentAuditId(Integer updatedAuditPostStatus, Integer agentAuditId) {
        return agentAuditMapper.updateAuditPostStatusByAgentAuditId(updatedAuditPostStatus, agentAuditId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPostUserId(Integer postUserId) {
        return Message.success(null).add(agentAuditMapper.selectByPostUserId(postUserId));
    }

    ConsoleUser init(AgentAudit agentAudit) {
        ConsoleUser consoleUser = new ConsoleUser();

        if (agentAudit.getUsername() != null)
            consoleUser.setUsername(agentAudit.getUsername());

        if (agentAudit.getPassword() != null)
            consoleUser.setPassword(agentAudit.getPassword());

        if (agentAudit.getUserGroup() != null)
            consoleUser.setUserGroup(agentAudit.getUserGroup());

        consoleUser.setRegistrationTime(new Date());

        if (agentAudit.getOrgId() != null)
            consoleUser.setOrgId(agentAudit.getOrgId());

        if (agentAudit.getRealname() != null)
            consoleUser.setRealname(agentAudit.getRealname());

        if (agentAudit.getUserPhone() != null)
            consoleUser.setUserPhone(agentAudit.getUserPhone());

        if (agentAudit.getState() != null)
            consoleUser.setState(agentAudit.getState());

        if (agentAudit.getSex() != null)
            consoleUser.setSex(agentAudit.getSex());

        if (agentAudit.getEducation() != null)
            consoleUser.setEducation(agentAudit.getEducation());

        if (agentAudit.getIdcard() != null)
            consoleUser.setIdcard(agentAudit.getIdcard());

        if (agentAudit.getEmail() != null)
            consoleUser.setEmail(agentAudit.getEmail());

        if (agentAudit.getBankName() != null)
            consoleUser.setBankName(agentAudit.getBankName());

        if (agentAudit.getAccountName() != null)
            consoleUser.setAccountName(agentAudit.getAccountName());

        if (agentAudit.getAccountId() != null)
            consoleUser.setAccountId(agentAudit.getAccountId());

        if (agentAudit.getRemarks() != null)
            consoleUser.setRemarks(agentAudit.getRemarks());

        if (agentAudit.getLevel() != null)
            consoleUser.setLevel(agentAudit.getLevel());

        if (agentAudit.getSubordinateId() != null)
            consoleUser.setSubordinateId(agentAudit.getSubordinateId());

        if (agentAudit.getFile() != null)
            consoleUser.setFile(agentAudit.getFile());

        if (agentAudit.getAgentLeader() != null)
            consoleUser.setAgentLeader(agentAudit.getAgentLeader());

        return consoleUser;
    }

    public boolean agree(AgentAudit agentAudit) {
        //删除操作
        if (agentAudit.getAgentAuditOperation() == 0) {
            List<ConsoleUser> consoleUserList = consoleUserMapper.selectByUserGroup1(2);
            for (ConsoleUser consoleUser : consoleUserList) {
                if (consoleUser.getSubordinateId() != null) {
                    String ids[] = consoleUser.getSubordinateId().split(";");
                    int flag = 0;
                    String idss = "";
                    for (String id : ids) {
                        if (agentAudit.getUserId() == Integer.parseInt(id)) {
                            flag = 1;
                        } else {
                            idss = idss + id + ";";
                        }
                    }
                    if (flag == 1) {
                        consoleUserMapper.updateSubordinateIdByUserId(idss, consoleUser.getUserId());
                    }
                }
            }
            return consoleUserMapper.deleteByPrimaryKey(agentAudit.getUserId()) > 0 ? true : false;
        }
        //添加操作
        else if (agentAudit.getAgentAuditOperation() == 1) {
            ConsoleUser consoleUserBoss = consoleUserMapper.selectByPrimaryKey(agentAudit.getPostUserId());
            //代理人添加
            if (consoleUserBoss.getUserGroup() == 2) {
                ConsoleUser consoleUser = init(agentAudit);
                if (consoleUserMapper.insertSelective(consoleUser) > 0) {
                    int id = consoleUser.getUserId();
                    if (consoleUserBoss.getSubordinateId() != null) {
                        String ids = consoleUserBoss.getSubordinateId() + id + ";";
                        return consoleUserMapper.updateSubordinateIdAndAgentLeaderByUserId(ids, 1,agentAudit.getPostUserId()) > 0 ? true : false;
                    } else {
                        String ids = id + ";";
                        return consoleUserMapper.updateSubordinateIdAndAgentLeaderByUserId(ids, 1,agentAudit.getPostUserId()) > 0 ? true : false;
                    }

                } else return false;
            }
            //系统管理员或管理员进行添加
            else {
                ConsoleUser consoleUser = init(agentAudit);
                return consoleUserMapper.insertSelective(consoleUser) > 0 ? true : false;
            }


        } else return false;
    }


}





